<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/18
 * Time: 16:47
 */

namespace App\Http\Controllers;


use App\Blogs;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BlogController extends Controller
{


    /**
     * 博客首页
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function index()
    {
        $rows = Blogs::orderBy('created_at', 'desc')->paginate(5);

        return view('blog/index', compact('rows'));
    }


    /**
     * 文章详情
     *
     * @param Blogs $info 路由模型绑定id
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function info(Blogs $info)
    {


        return view('blog/info', compact('info'));

    }

    /**
     * 创建文章
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function create()
    {

        // 检查用户是否登录
        if (\Auth::check() === false) {
            return back()->withErrors('请先登录!');
        }


        return view('blog/create');
    }


    /**
     * 创建文章逻辑
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect|void
     */
    public function add(Request $request)
    {
        // 验证
        $this->validate($request, [
            'title'   => 'required',
            'content' => 'required',
        ]);

        // 逻辑

//        $param = [
//            'user_id' => \Auth::id(),
//            'title'   => $request->input('title'),
//            'content' => $request->input('content'),
//        ];

        $blog = new Blogs();
        $blog->user_id = \Auth::id();
        $blog->title = $request->input('title');
        $blog->content = $request->input('content');


        if ($blog->save() === true) {
            // 渲染
            return redirect('/');
        }

        return back()->withErrors('文章发布失败！');


    }


    /**
     * 》》设置文件上传保存位置
     * 》》1. 存放位置： storage/app/public
     * 》》2. 修改配置文件 config/filesystems.php 将 'default' => env('FILESYSTEM_DRIVER', 'local'),修改为 'default' => env('FILESYSTEM_DRIVER', 'public'),
     * 》》3. 使用链接命令： php artisan storage:link
     *
     * 注意开启 php_fileinfo 扩展
     */
    public function upload()
    {

        // 验证图片是否存在
        if (!request()->hasFile('wangEditorH5File')) {
            //            request()->session()->flash('error_msg','文件不存在');
            return false;
        }

        $path = request()->file('wangEditorH5File')->store(date('Y-m-d'));
        // 获取后缀名
        //        $ext = $img->extension();
        //新文件名
        //        $saveName =time().rand().".".$ext;
        //


        return asset('uploads/' . $path);

    }


    /**
     * 修改文章
     *
     * @param Blogs $info
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View|\think\response\View
     */
    public function edit(Blogs $info)
    {

        return view('blog/edit', compact('info'));

    }


    /**
     *修改文章逻辑
     *
     * @param Request $request
     * @param Blogs   $info
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect|void
     */
    public function update(Request $request, Blogs $info)
    {

        // 用户权限验证
        $this->authorize('edit', $info);

        // 验证表达
        $this->validate($request, [
            'title'   => 'required|max:255',
            'content' => 'required',
        ]);

        // 更新逻辑
        $info->title = $request->input('title');
        $info->content = $request->input('content');
        $info->save();

        // 渲染视图
        return redirect("/info/{$info->id}");
    }

    /**
     * 删除文章
     *
     * @param Blogs $info
     *
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect|void
     * @throws \Exception
     */
    public function delete(Blogs $info)
    {


        // 用户权限验证
        $this->authorize('delete', $info);

        $info->delete();

        return redirect("/");
    }


}